<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-s-custom"></i> 学院管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>

                <!--选择学院查询-->
                <el-input v-model="query.aname" placeholder="学院名" class="handle-input mr10"></el-input>

                <!--选择报道日期查询-->
                <el-date-picker
                    v-model="date"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    unlink-panels='true'
                class='mr10'>
                </el-date-picker>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="danger" icon="el-icon-plus" class="handle-del mr10" @click="handleAdd">新增</el-button>
            </div>

            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="aname" label="学院"></el-table-column>
                <el-table-column prop="place" label="报到地点"></el-table-column>
                <el-table-column prop="time" label="报道时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-tickets"
                            @click="classView(scope.$index, scope.row)"
                        >查看班级</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                    :current-page="query.pageIndex"
                    :page-sizes="[10, 50, 100, 500]"
                    :page-size="query.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">

                <el-form-item label="学院">
                    <el-input v-model="form.aname"></el-input>
                </el-form-item>
                <el-form-item label="报到时间">
                    <el-input v-model="form.time"></el-input>
                </el-form-item>
                <el-form-item label="报到地点">
                    <el-input v-model="form.place"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="done">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'testTable',
    data() {
        return {
            title: "",
            query: {
                //提供查询项：学院
                aname: '',
                place: '',
                time: '',
                ano:'',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            editVisible: false,
            pageTotal: 0,
            form: {},

            //日期选择器数据
            date:''
        };
    },
    created() {
        this.getData();
        // this.getAoptions();
    },
    methods: {
        // 获取所有学院的数据
        getData() {
            this.$http.post('/academy/selectAll',{

                "currentPage": this.query.pageIndex,
                "pageSize": this.query.pageSize,
                "placeK": this.query.place,
                "time":this.query.time,
                "timeBegin":this.date[0],
                "timeEnd": this.date[1],
                "anameK":this.query.aname

                }).then((res)=>{//回调函数
                    this.tableData = res.data.records
                    this.pageTotal = res.data.totalElements;
            })
        },

        //获得班级下拉框数据
        // getAoptions(){
        //     this.$http.get('/academy',{
        //        }).then((res)=>{
        //         console.log(res)
        //     })
        // },

        // 触发搜索按钮
        handleSearch() {
            console.log(this.date)
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },

        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('/academy/' + row.ano).then((res)=>{//回调函数
                        if (res.data.code === 200) {
                            this.getData();
                            this.$message.success('删除成功');
                        }else {
                            this.$message.error(res.data.message);
                        }
                    })
                })
        },
        // todo 查看学院下属班级
        classView(index,row){
            // const aca=row.aname;
            this.$router.push({
                path: "/classManage",
                query: {
                    row: row
                }
            });
        },

        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            let ids = this.multipleSelection.map(item => item.ano)
            this.$http.delete('/academy/batchDelete', {data: ids}).then((res)=>{//回调函数

                this.$message.error(`删除了` + res.data.data + '条数据');
                this.getData();

        })

            this.multipleSelection = [];
        },

        // 编辑操作
        handleEdit(index, row) {
            this.title = "编辑"
            //生成row的副本，防止修改原数据
            this.form = Object.assign({}, row);
            this.editVisible = true;
        },
        //点击确定按钮判断是保存还是更新
        done(){
            if(this.title==="编辑"){//弹出框title是编辑就是更新
                this.edit()
            }else {
                this.save()
            }
        },

        // 新增操作
        handleAdd(){
            this.form = {};
            this.title = "新增学院";
            this.editVisible = true;
        },

        // 新增学院
        save() {
            this.$http.post("/academy", this.form)
                .then((res)=>{
                    if (res.data.code === 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },

        // 编辑学院信息
        edit() {
            this.$http.put("/academy", this.form)
                .then((res)=>{
                    if (res.data.code === 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },

        // 每页条数更改
        handleSizeChange(val) {
            this.$set(this.query, 'pageIndex', 1);
            this.$set(this.query, 'pageSize', val);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-input {
    width: 180px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}

</style>
